{extend name="platform/base" /}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 添加用户 </header>
	<div class="panel-body">
		<form class="form-horizontal" id="user_form" name="form">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>姓名</label>
				<div class="col-md-2">
					<input type="text" class="form-control" onkeydown="error_hide()" id="admin_name" name="admin_name" value="" />
				</div>
				<p class="error">请输入用户姓名</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                 <label class="col-sm-2 control-label"><span class="color-red">*</span>密码</label>
	                 <div class="col-sm-2">
	                     <input type="password" name="user_password" onkeydown="error_hide()" id="user_password" class="form-control" />
	                 </div>
				<p class="error">请输入用户密码</p>
                                        </div>
            <div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>所属用户组</label>
				<div class="col-sm-2">
					<select id="group_id" name="group_id" class="form-control m-b"  onchange="error_hide()">
						<option value="">请选择用户组</option>
						{foreach name="auth_group" item="v1"}
							<option value="{$v1['group_id']}">&nbsp;&nbsp;{$v1['group_name']}</option>
						{/foreach}
					</select>
				</div>
				<p class="error">请选择用户组</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                                            <label class="col-sm-2 control-label">描述</label>
                                            <div class="col-sm-4">
                                                <div id="editor" class="form-control" style="overflow:scroll;height:150px;max-height:150px"></div>
                                            </div>
                                        </div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<!-- <button type="submit" class="btn btn-primary">保存</button> -->
 					<a href="javascript:void(0);" onclick="addUserAjax()" class="btn btn-primary">保存</a>
				</div>
			</div>
		</form>
	</div>
</section>
<script>
/* $().ready(function() {
	$("#user_form").validate({
		submitHandler:function(){
			addUserAjax();
		}
	});
}); */
	// 防止重复提交 flag为true则表示正在提交 flag为false则表示不是正在提交
	var flag = false;
	/**
	 *添加用户
	 **/
	function addUserAjax() {
		var admin_name = $("#admin_name").val();
		var group_id = $("#group_id").val();
		var user_password = $("#user_password").val();
		var desc = $("#editor").html();
	
		if(verify() && !flag){
			flag = true;
			$.ajax({
				type : "post",
				url : "{:__URL('PLATFORM_MAIN/auth/adduser')}",
				data : {
					'admin_name' : admin_name,
					'group_id' : group_id,
					'user_password' : user_password,
					'desc' : desc
				},
				async : true,
				success : function(data) {
					flag = false;
					if (data['code'] > 0) {
						//location.href = "{:__URL('PLATFORM_MAIN/auth/userlist')}";
						showMessage('success', data['message'], "{:__URL('PLATFORM_MAIN/Auth/userList')}");
					}else{
						/* $("#dialog").dialog({
							buttons : {
								"确定,#e57373" : function() {
									$(this).dialog('close');
									flag = false;
								}
							},
							contentText : data["message"]
						}); */
						showMessage('error', data['message']);
					}
				}
			});
		}
		
	}
	// 添加或修改的验证
	function verify(){
		
		var admin_name = $("#admin_name").val();
		var group_id = $("#group_id").val();
		var user_password = $("#user_password").val();
		var desc = $("#editor").html();
		
		var reg_name = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？_]"); 
		if($.trim(admin_name) == ''){
			$("#admin_name").focus().parent().next().html('请输入用户名').show();
			return false;
		}else if(reg_name.test(admin_name)){
			$("#admin_name").focus().parent().next().html('用户名不能包含特殊字符').show();
			return false;
		}
		
		if($.trim(user_password) == ''){
			$("#user_password").focus().parent().next().html('请输入用户密码').show();
			return false;
		}
		
		if(group_id == ''){
			$("#group_id").focus().parent().next().show();
			return false;
		}
		
		return true;
	}
</script>
{/block}